package com.hippo.core.ui.compose.register

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewState
import com.hippo.core.R
import com.hippo.core.config.AppConfig
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.theme.HippoTheme


@Composable
fun UserAgreementDialog(onDismissRequest: () -> Unit, onAgree: () -> Unit) {

//    BottomSheetDialog(
//        title = stringResource(id = R.string.me_5),
//        onDismissRequest = onDismissRequest
//    ) {
//        DialogContent(onDismissRequest,onAgree)
//    }

    Dialog(
        onDismissRequest = onDismissRequest,
        properties = androidx.compose.ui.window.DialogProperties(usePlatformDefaultWidth = false)
    ) {
        DialogContent(onDismissRequest, onAgree)
    }

}

@Composable
private fun DialogContent(onDismissRequest: () -> Unit, onAgree: () -> Unit) {
    val webViewState = rememberWebViewState(AppConfig.USER_POLICY)
    Column(
        modifier = Modifier
            .padding(top = 50.dp)
            .fillMaxSize()
            .background(Color.White)
    ) {


        Box(
            modifier = Modifier
                .fillMaxWidth()
        ) {

            Text(
                text = stringResource(id = R.string.me_5),
                modifier = Modifier.align(Alignment.Center)
            )

            IconButton(
                modifier = Modifier.align(Alignment.CenterEnd),
                onClick = onDismissRequest,
            ) {
                Icon(
                    imageVector = Icons.Default.Close,
                    contentDescription = "Close",
                    tint = MaterialTheme.colorScheme.primary
                )
            }

        }

        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {


            Image(
                modifier = Modifier.size(75.dp),
                painter = painterResource(id = R.mipmap.protocol),
                contentDescription = null
            )
            Spacer(modifier = Modifier.height(30.dp))

            Text(
                modifier = Modifier.padding(top = 16.dp),
                text = stringResource(R.string.protocol_1),
                style = TextStyle(
                    color = Color.Black,
                    fontWeight = FontWeight.Bold,
                    fontSize = 25.sp,
                )
            )


            WebView(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f),
                state = webViewState,
                onCreated = {
                    it.settings.apply {
                        javaScriptEnabled = true // 启用 JavaScript
                        loadWithOverviewMode = true // 适应屏幕宽度
                        useWideViewPort = true // 启用宽视口
                        setSupportZoom(true) // 允许缩放
                        builtInZoomControls = true // 显示缩放控件
                        displayZoomControls = false // 隐藏缩放控件
                    }
                }
            )


            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            ) {
                ThemeButton(text = stringResource(id = R.string.agree)) {
                    onAgree()
                }
            }
        }


    }
}


@Preview
@Composable
private fun DialogContentPreview() {
    HippoTheme {
        DialogContent({}, {})
    }
}